﻿using System;
using System.Windows.Forms;
using System.Globalization;
using System.Collections.Generic;

namespace DataUpload
{
    public partial class FrmCultureInfo : Form
    {
        private CultureInfo[] _cultureInfo;

        public FrmCultureInfo()
        {
            InitializeComponent();
        }

        private void FrmCultureInfo_Load(object sender, EventArgs e)
        {
            // Get available culture types
            //CultureTypes[] cultureTypes = (CultureTypes[])Enum.GetValues(typeof(CultureTypes));
            //foreach (CultureTypes ctype in cultureTypes)
            //    cboCultureType.Items.Add(ctype);
            //cboCultureType.Text = "SpecificCultures";
            cboCultureType.Items.Add(CultureTypes.SpecificCultures);
            cboCultureType.SelectedIndex = 0;
        }

        private void cboCultureType_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Get available cultures associated to selecting culture type
            _cultureInfo = CultureInfo.GetCultures(CultureTypes.SpecificCultures);
            string[] cultureDisplayName = new string[_cultureInfo.Length];
            for (int i = 0; i < cultureDisplayName.Length; i++)
                cultureDisplayName[i] = _cultureInfo[i].EnglishName;
            Array.Sort<string, CultureInfo>(cultureDisplayName, _cultureInfo);
            cboCultureDisplayName.DataSource = cultureDisplayName;
            cboCultureDisplayName.SelectedIndex = 0;
        }

        private void cboCultureDisplayName_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtCultureName.Text = _cultureInfo[cboCultureDisplayName.SelectedIndex].Name;
            txtDecimal.Text = _cultureInfo[cboCultureDisplayName.SelectedIndex].NumberFormat.NumberDecimalSeparator;
            txtDigit.Text = _cultureInfo[cboCultureDisplayName.SelectedIndex].NumberFormat.NumberGroupSeparator;
            txtDateFormat.Text = _cultureInfo[cboCultureDisplayName.SelectedIndex].DateTimeFormat.ShortDatePattern;
            txtTimeFormat.Text = _cultureInfo[cboCultureDisplayName.SelectedIndex].DateTimeFormat.LongTimePattern;
        }

        private void FrmCultureInfo_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (DialogResult == DialogResult.OK)
            {
                if (cboCultureType.SelectedIndex == -1)
                {
                    MessageBox.Show("Culture type must be \"SpecificCultures\"!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    e.Cancel = true;
                    return;
                }

                if (cboCultureDisplayName.SelectedIndex == -1)
                {
                    MessageBox.Show("Culture must be one in the list!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    e.Cancel = true;
                }
            }
        }
    }
}
